package stu.day9_day18;


/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class LeetCode160 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode node1 = headA;
        ListNode node2 = headB;
        int num = 0;
        while (num<4){
            if(node1==node2){
                num = 4;
                return node1;
            }else {
                if(node1.next==null){
                    num += 1;
                }
                if(node2.next==null){
                    num += 1;
                }
                node1 = node1.next==null?headB:node1.next;
                node2 = node2.next==null?headA:node2.next;

            }

        }

        return null;

    }
}